package cn.isaac.firstkotlin.ui

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import cn.isaac.firstkotlin.R
import cn.isaac.firstkotlin.domain.Request
import cn.isaac.firstkotlin.adapter.ForecastListAdapter2
import cn.isaac.firstkotlin.clazz.Personn
import cn.isaac.firstkotlin.domain.RequestForecastCommand
import cn.isaac.firstkotlin.practice.supportsLollipop
import org.jetbrains.anko.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    companion object {
        val foreUlr = "https://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650823035&idx=1&sn=2db4eea9eb569bf375f847f36b1229fa&chksm=80b78de5b7c004f3c3f0a54b535671971f91708def1faecb4b1a4628bb7efb9d00392f11beca&mpshare=1&scene=1&srcid=0519WIUyTFOAlYUMtfe9ZMFM&pass_ticket=XASJ2rHhIaYcM3tA57YvPva6Sopozpga8vcqDgFe5cw%2FgEsPuNtinjKfkX1X60oj#rd"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView.setTextColor(R.color.red)
        textView.text = "Hello Kotlin!!"
        textView.setTextSize(30.5f)

        val personn = Personn()
        personn.name = "Isaac"
        personn.surname = "Rao"
        textView.text = personn.name + "   " + personn.surname

        forecastList.layoutManager = LinearLayoutManager(this)

        async {
            val result = RequestForecastCommand("94043").execute()
            uiThread {
                forecastList.adapter = ForecastListAdapter2(result) {toast(it.date)}
            }
        }

        async {
            Request(foreUlr).run()
            uiThread { longToast("Request performed") }
        }

        //应用自定义的内联函数
        supportsLollipop {
            window.statusBarColor = Color.GRAY
        }

        val list = listOf(1,2,3,4,5,6,7,8,9)
        list.any{it > 10}
        list.all { it > 10 }
        list.count { it % 2 == 0 }
        list.fold(10) {t, n -> t + n}
        list.foldRight(10) {t, n -> t + n}
        list.foldIndexed(10){index, t, n -> t + n}
        list.foldRightIndexed(10){index, i, acc -> index + i + acc}
        list.forEach { println(it) }
        list.forEachIndexed { index, i -> println("position $index contains a $i") }
        list.maxBy { it + 10 }
        list.max()
        list.min()
        list.minBy { it - 10 }
        list.none()
        list.none { it > 10 }
        list.reduce { acc, i -> acc + i }
        list.reduceRight { i, acc -> acc + i }
        list.reduceRightIndexed { index, i, acc -> index + i + acc }
        list.sumBy { if (it > 5) it else -it }
        val drop = list.drop(3)
        list.dropWhile { it < 3 }
        list.dropLastWhile { it < 3 }
        list.dropLast(3)
        list.filter { it % 2 == 0 }
        list.filterNot { it % 2 != 0 }
        list.filterNotNull()
        list.slice(listOf(1,3,4))
        list.take(2)
        list.takeWhile { it < 3 }
        list.flatMap { listOf(it, it + 1, it + 3) }
        list.groupBy { if (it % 2 == 0) "even" else "odd" }
        list.map { it * 2 }
        list.mapIndexed { index, i -> index * 2 }
        list.mapNotNull { it * 2 }
        list.contains(2)
        list.elementAt(2)
        list[2]
        list.get(2)
        list.elementAtOrElse(10, {it * 2})
        list.elementAtOrNull(10)
        list.first { it % 2 == 0 }
        list.singleOrNull { it % 2 == 0 }

        val listRepeated = listOf<Int>(2,2,3,4,5,5,5,6,6)
        list.zip(listRepeated) {it1 ,it2 -> it1 + it2}  //merge被废弃了
        list.partition { it % 2 == 0 }

        list.reversed()
        list.sorted()
        list.sortedDescending()
        list.sortedBy { it % 3 }

    }

}
